package task

import (
	"errors"
	"fmt"
	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/logs"
	"io/ioutil"
	"os"
)

func DeletLogs() error {
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("DeletLogs, config init, error:", err)
		return err
	}
	logDir := BConfig.String("log::log_dir")
	if logDir == "" {
		logs.Error("DeletLogs, config excel::log_dir, error: invalid value")
		return errors.New("value is nil")
	}
	fileInfoList, err := ioutil.ReadDir(logDir)
	if err != nil {
		logs.Error(err)
		return err
	}
	fmt.Println(len(fileInfoList))
	for i := range fileInfoList {
		if fileInfoList[i].Name() != "cve.log" {
			err := os.Remove(logDir + "/" + fileInfoList[i].Name())
			if err != nil {
				logs.Error("Failed to delete file: ", fileInfoList[i].Name())
			}
		}
	}
	return nil
}

// ProcLogData Processing log
func ProcLogData() error {
	logs.Info("Create log task starts")
	// delete logs
	err := DeletLogs()
	if err != nil {
		logs.Error(err)
	}
	logs.Info("End of log creation task")
	return err
}
